package com.fl.prestagil.http;

import com.fl.prestagil.constant.AppConfigConstant;
import com.fl.prestagil.http.request.BaseGoRequest;
import com.fl.prestagil.http.request.BaseToRequest;
import com.fl.prestagil.http.request.CheckAppVersionRequest;
import com.fl.prestagil.http.request.GetUploadFileListRequest;
import com.fl.prestagil.http.request.LoginRequest;
import com.fl.prestagil.http.request.QueryAppHomeAppDcProductBaseInfoRequest;
import com.fl.prestagil.http.request.QuerySessionBasicInfoRequest;
import com.fl.prestagil.http.request.SendLoginCodeRequest;
import com.fl.prestagil.http.request.SendVoiceLoginCodeRequest;
import com.google.gson.Gson;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 接口调用客户端类
 * @author wzs
 * @date 2024/5/5
 */
public class HttpClient {
    private static final String TAG = "HttpClient";

    private static OkHttpClient client;

    public static OkHttpClient getClient() {
        if (client == null) {
            client = new OkHttpClient.Builder().build();
        }
        return client;
    }

    public static Call getPostCall(String url, Object o) {
        BaseToRequest toRequest = new BaseToRequest(o);
        BaseGoRequest goRequest = new BaseGoRequest(toRequest);
        Gson gson = new Gson();
        String json = gson.toJson(goRequest);
        MediaType mediaType = MediaType.get("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(mediaType, json);

        Request request = new Request.Builder()
                .url(AppConfigConstant.API_URL + url)
                .post(body)
                .build();

        return HttpClient.getClient().newCall(request);
    }

    /**
     * 校验应用版本更新配置
     * @param request
     * @return
     */
    public static Call checkAppVersion(CheckAppVersionRequest request) {
        String url = "/app/checkAppVersion";
        return getPostCall(url, request);
    }

    /**
     * 查询App首页贷超产品信息
     * @param request
     * @return
     */
    public static Call queryAppHomeAppDcProductBaseInfo(QueryAppHomeAppDcProductBaseInfoRequest request) {
        String url = "/app/product/queryAppHomeAppDcProductBaseInfo";
        return getPostCall(url, request);
    }

    /**
     * APP发送登录验证码
     * @param request
     * @return
     */
    public static Call sendLoginCode(SendLoginCodeRequest request) {
        String url = "/app/sendLoginCode";
        return getPostCall(url, request);
    }

    /**
     * APP发送登录语音验证码
     * @param request
     * @return
     */
    public static Call sendLoginCode(SendVoiceLoginCodeRequest request) {
        String url = "/app/voice/verify/sendLoginCode";
        return getPostCall(url, request);
    }

    /**
     * APP用户登录
     * @param request
     * @return
     */
    public static Call login(LoginRequest request) {
        String url = "/app/login";
        return getPostCall(url, request);
    }

    /**
     * 查询需要上传的文件
     * @param request
     * @return
     */
    public static Call getUploadFileList(GetUploadFileListRequest request) {
        String url = "/app/getUploadFileList";
        return getPostCall(url, request);
    }

    /**
     * 查询APP操作埋点需要信息
     * @param request
     * @return
     */
    public static Call querySessionBasicInfo(QuerySessionBasicInfoRequest request) {
        String url = "/app/querySessionBasicInfo";
        return getPostCall(url, request);
    }
}
